package cn.edu.hstc.wm.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import cn.edu.hstc.wm.Constants;
import cn.edu.hstc.wm.R;
import cn.edu.hstc.wm.service.AppConfigService;

/**
 * @ClassName: SplashActivity
 * @Description: 启动动画效果页面
 * 
 * @ModificationHistory
 */
public class SplashActivity extends Activity {
	private static final int GO_HOME = 00;
	private static final int GO_GUIDE = 0x01;
	/** 延迟时间 */
	private static final long SPLASH_DELAY_MILLIS = 500;
	private boolean isFirstIn = false;

	/**
	 * Handler:
	 */
	private Handler mHandler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case GO_HOME:
				goHome();
				break;
			case GO_GUIDE:
				goGuide();
				break;
			}
			super.handleMessage(msg);
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		View view = View.inflate(this, R.layout.splash, null);
		setContentView(view);

		// 载入动画
		Animation animation = AnimationUtils.loadAnimation(this, R.anim.start_anim);

		// 设置启动动画
		view.startAnimation(animation);

		// 动画监听
		animation.setAnimationListener(new AnimationListener() {

			// 动画开始
			@Override
			public void onAnimationStart(Animation arg0) {
			}

			// 动画重复
			@Override
			public void onAnimationRepeat(Animation arg0) {
			}

			// 动画结束
			@Override
			public void onAnimationEnd(Animation arg0) {
				//
				init();
			}
		});
	}

	private void init() {
		// 从SharedPreferences读取配置信息

		// 第一次启动,没有该配置文件,则使用默认值 true
		isFirstIn = AppConfigService.getBoolValFromConf(this, Constants.IS_FIRST_IN);

		if (isFirstIn) {
			// 进入引导界面
			mHandler.sendEmptyMessageDelayed(GO_GUIDE, SPLASH_DELAY_MILLIS);
		} else {
			// 进入主界面
			mHandler.sendEmptyMessageDelayed(GO_HOME, SPLASH_DELAY_MILLIS);
		}

	}

	private void goHome() {
		Intent intent = new Intent(SplashActivity.this, MainActivity.class);
		startActivity(intent);
		finish();
	}

	private void goGuide() {
		Intent intent = new Intent(SplashActivity.this, AppGuide.class);
		intent.putExtra(Constants.START_GUIDE_PAGE_TYPE, Constants.START_GUIDE_PAGE_TYPE_FIRST_IN);
		startActivity(intent);
		finish();
	}
}